package com.cn.hospital.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.cn.hospital.message.Msg;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Log4j2
public class ExceptionCatch {

    @ResponseBody
    @ExceptionHandler(TokenException.class)
    public Msg tokenException(TokenException e){
        e.printStackTrace();
        log.error("catch tokenException : {}",e.getMessage());
        return Msg.okResult(404,"token无效/已过期!");
    }

    @ResponseBody
    @ExceptionHandler(NotLoginException.class)
    public Msg notLoginException(NotLoginException e){
        e.printStackTrace();
        log.error("catch notLoginException : {}",e.getMessage());
        return Msg.okResult(404,"请先登录在操作!");
    }

    @ResponseBody
    @ExceptionHandler(FourZeroFourException.class)
    public Msg fourZeroFourException(FourZeroFourException e){
        e.printStackTrace();
        log.error("catch fourZeroFourException : {}",e.getMessage());
        return Msg.okResult(404,"路径找不到,请重试!");
    }

    @ResponseBody
    @ExceptionHandler(NotPermissionException.class)
    public Msg notPermissionException(NotPermissionException e){
        e.printStackTrace();
        log.error("catch notPermissionException : {}",e.getMessage());
        return Msg.okResult(403,"您没有权限访问!");
    }

    @ResponseBody
    @ExceptionHandler(NotRoleException.class)
    public Msg notPermissionException(NotRoleException e){
        e.printStackTrace();
        log.error("catch notPermissionException : {}",e.getMessage());
        return Msg.okResult(403,"您没有权限访问!");
    }
    /**
     * 拦截全部异常Exception一定要放最下面
     *  否则全部返回本方法返回内容
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Msg exception(Exception e){
        e.printStackTrace();
        log.error("catch exception : {}",e.getMessage());
        return Msg.okResult(404,"服务器出现异常,请稍后再试");
    }
}
